package com.corvo.pulsor;

import java.io.IOException;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.StateBasedGame;

import resources.ResourceManager;
import states.States;
import states.gameplay.GameplayState;
import states.loading.BuildGameAfterLoading;
import states.loading.LoadingGameState;
import states.mainmenu.MainMenuGameState;

public class PulsorGame extends StateBasedGame{
	
	private static final String FILES_GAMERESOURCES = "/resources.xml";

	private static final Logger _tracer = Logger.getLogger(PulsorGame.class.getName());  
	
	private static final String version = "v1.0";
	private static final String gameName = "Ludum Dare 26 - Minimalism - PULSOR";
	public PulsorGame() {
		
		super(PulsorGame.gameName + "-" + PulsorGame.version);
	}

	@Override
	public void initStatesList(GameContainer gc) throws SlickException {
		// Resource loader
		ResourceManager rm = new ResourceManager(0);
		
		LoadingGameState ls = null;
		ls = new LoadingGameState(States.LOADING_STATE_ID);
		ls.setResourceManager(rm);
		
		try {
			ls.setLoadData(PulsorGame.class.getResourceAsStream(FILES_GAMERESOURCES));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		addState( ls );
		
		ls.setAfterLoading(new BuildGameAfterLoading(rm));
		
		// Main menu State
		MainMenuGameState mm = new MainMenuGameState(States.MAIN_MENU_ID, rm);
		addState(mm);
	
		// Deploy State
		GameplayState gs = new GameplayState(States.MISSION_STATE_ID);
		gs.setResourceManager(rm);
		addState(gs);
		
		// set the loading state
		enterState(States.LOADING_STATE_ID);
	}
	
	public static void main(String[] args) throws SlickException, SecurityException, IOException
    {
		//starting the app
        AppGameContainer app = new AppGameContainer( new PulsorGame(), 1024, 768, false );
        
        // starting the logs
        FileHandler fh = new FileHandler("game.log", true);
        _tracer.addHandler(fh);
        SimpleFormatter formatter = new SimpleFormatter();
        fh.setFormatter(formatter);
        _tracer.setLevel(Level.ALL);
         
        // Application properties
        //app.setShowFPS(false);
        app.setDisplayMode(1024, 768, false);
        _tracer.info("Setting display mode " + app.getWidth() + "x" + app.getHeight() + ", fullscreen : " + app.isFullscreen());
         
        //app.setTargetFrameRate(60);
        //app.setSmoothDeltas(true);
        //app.setFullscreen(true);
        _tracer.info("Starting the game..." + gameName + " version: " + version);
         
        app.start();
    }

}
